<style lang="less">
    @import './login.less';
</style>


<template>
    <div class="login" @keydown.enter="handleSubmit">
        <div class="login-con">
            <Card :bordered="false">
                <p slot="title">
                    <Icon type="log-in"></Icon>
                    欢迎登录
                </p>
                <div class="form-con">
                    <Form ref="loginForm" :model="form" :rules="rules">
                        <FormItem prop="userName">
                            <Input v-model="form.userName" placeholder="请输入用户名">
                                <span slot="prepend">
                                    <Icon :size="16" type="person"></Icon>
                                </span>
                            </Input>
                        </FormItem>
                        <FormItem prop="password">
                            <Input type="password" v-model="form.password" placeholder="请输入密码">
                                <span slot="prepend">
                                    <Icon :size="14" type="locked"></Icon>
                                </span>
                            </Input>
                        </FormItem>
                        <div class="movebox" >
                            <div class="movego"></div>
                            <div class="txt" id="txt">拖动滑块验证</div>
                            <div class="move moveBefore" v-move></div>
                         </div>
                        <FormItem>
                            <Button @click="handleSubmit" type="primary" long>登录</Button>
                        </FormItem>
                    </Form>
                </div>
            </Card>
        </div>
    </div>
</template>

<script>
import Cookies from 'js-cookie';
import $ from 'jquery';
import bgjs from '@/views/canvas-nest.js';
export default {
    data () {
        return {
            form: {
                userName: '',
                password: ''
            },
            rules: {
                userName: [
                    { required: true, message: '账号不能为空', trigger: 'blur' }
                ],
                password: [
                    { required: true, message: '密码不能为空', trigger: 'blur' }
                ]
            }
        };
    },
    methods: {
        handleSubmit () {
            this.$refs.loginForm.validate((valid) => {
                if (valid) {
                    var that=this;
                    if($('#txt').text()=="验证通过"){
                        this.axios({
                            method: 'post',
                            url: that.$store.state.app.globalUrl+'/admin/Login/index',
                            data: {
                                username: this.form.userName,
                                password: this.form.password
                            }
                        }).then(function(res){
                            if(res.data.msg=="登录成功"){
                                Cookies.set('user', that.form.userName);
                                Cookies.set('password', that.form.password);
                                that.$store.commit('setAvator', res.data.data.headImg);
                                // if (this.form.userName === 'iview_admin') {
                                Cookies.set('access', 0);
                                // } else {
                                //     Cookies.set('access', 1);
                                // }
                                that.$router.push({
                                    name: 'home_index'
                                });
                            }else{
                                that.$Message.error('用户名或者密码错误！');
                            }
                        });
                    }else{
                        that.$Message.error('请滑动解锁！');
                    }
                   
                }
            });
        }
    },
    mounted(){

    },
    directives: {
        move(el) {
            el.onmousedown = function(e) {
                var X = e.clientX - el.offsetLeft
                document.onmousemove = function(e) {
                    var endx = e.clientX - X
                    el.className = 'move moveBefore'
                    el.style.left = endx + 'px'
                    // console.log(el.parentNode.children[0])
                    var width = $('.movebox').width() - $('.move').width()
                    el.parentNode.children[0].style.width = endx + 'px'
                    el.parentNode.children[1].innerHTML = '拖动滑块验证'
                    //临界值小于
                    if (endx <= 0) {
                        el.style.left = 0 + 'px'
                        el.parentNode.children[0].style.width = 0 + 'px'
                    }
                    //临界值大于
                    if (parseInt(el.style.left) >= width) {
                        el.style.left = width + 'px'
                        el.parentNode.children[0].style.width = width + 'px'
                        el.parentNode.children[1].innerHTML = '验证通过'
                        el.className = 'move moveSuccess'
                        el.onmousedown = null
                    }
                }
            }
            document.onmouseup = function() {
                document.onmousemove = null
            }
        }
    }
};
</script>

<style>

</style>
